<?php 

class User_Model extends grocery_CRUD_Model 
{
    
    function get_list()
    {
    	if($this->table_name === null)
    		return false;
    	
    	$select = "{$this->table_name}.*";
    	if(!empty($this->relation))
    		foreach($this->relation as $relation)
    		{
    			list($field_name , $related_table , $related_field_title) = $relation;
    			$unique_join_name = $this->_unique_join_name($field_name);
    			$unique_field_name = $this->_unique_field_name($field_name);
    			
				if(strstr($related_field_title,'{'))
    				$select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
    			else    			
    				$select .= ", $unique_join_name.$related_field_title as $unique_field_name";
    			
    			if($this->field_exists($related_field_title))
    				$select .= ", {$this->table_name}.$related_field_title as '{$this->table_name}.$related_field_title'";
    		}
        $this->db ->select('USUARIO.EMP_ID,USUARIO.USUA_ID,USUA_NOMBRES,USUA_APELLIDOS,USUA_CEDULA,USUA_CORREO,USUA_TELEFONO,USUA_CELULAR,USUA_DIRECCIONRESIDENCIA');
        $this->db->from('USUARIO');
        $this->db->join('FUNCIONARIO','FUNCIONARIO.USUA_ID=USUARIO.USUA_ID','LEFT OUTER');
        $this->db ->where('FUNCIONARIO.USUA_ID is null');        
    	$query = $this->db->get();	
        if($query->num_rows() > 0)
        {
            $result= $query->result();
        }
        else
        {
            $result= array();
        }        
        return $result;
    }
    
    /*
     * Recibe los valores ingresados en el formulario de crear
     */
    function db_insert($post_array)
    {
        $user_data['EMP_ID'] = 1;//se fija la empresa en uno porque no se maneja sucursales siempre
                                 //sera usuario de la unica empresa que sera registrada en al base de datos
        $user_data['USUA_NOMBRES'] = $post_array['USUA_NOMBRES'];
        $user_data['USUA_APELLIDOS'] = $post_array['USUA_APELLIDOS'];
        $user_data['USUA_CORREO'] = $post_array['USUA_CORREO'];
        $user_data['USUA_CEDULA'] = $post_array['USUA_CEDULA'];
        $user_data['USUA_DIRECCIONRESIDENCIA'] = $post_array['USUA_DIRECCIONRESIDENCIA'];
        $user_data['USUA_TELEFONO'] = $post_array['USUA_TELEFONO'];
        $user_data['USUA_CELULAR'] = $post_array['USUA_CELULAR'];
        
    	$insert = $this->db->insert("USUARIO",$user_data);
    	if($insert)
    	{
           $id=$this->db->insert_id();
           if($insert)return $id;
           return false;
    	}
    	return false;
    }
    
    function loginUser($user,$password)
    {
        $this->load->library('encrypt');
        $key = '1234567890ABxa1b2c3d4f56g7h8i9j0kl1m2n31234567890ABCDEFGHIJKLMNOPQRSTVUWXYZa1b2c3d4f56g7h8i9j0kl1m2n3p4q6r7s8t9u0v1w2x3y4z.!"#$%&/()=?p4q6r7s8t9u0v1w2x3y4z.!"#$%&/()=?';
        $password_encrytp = $this->encrypt->encode($password, $key);
        $this->db->select('U.USUA_ID AS id,USUA_NOMBRES AS names,USUA_APELLIDOS AS lastnames,FUNC_CONTRASENA AS password,FUNC_USUARIO AS user,CAR_NOMBRE AS chargue');
        $this->db->from('USUARIO AS U');
        $this->db->join('FUNCIONARIO AS F','F.USUA_ID=U.USUA_ID','INNER');
        $this->db->join('FUNCIONARIODESEMPENIACARGO AS FDC','FDC.USUA_ID=F.USUA_ID','INNER');
        $this->db->join('CARGO AS C','FDC.CAR_ID=C.CAR_ID','INNER');
        $this->db->where('F.FUNC_USUARIO = ' . "'" . $user . "'");
        $this->db->where('F.FUNC_CONTRASENA = ' . "'" .$password_encrytp. "'");
        $this->db ->limit(1);
        
        $query = $this->db->get();
        
        if($query->num_rows() == 1)
        {
               return $query->result();
        }
        else
        {
                return false;
        }
    }
}

?>